﻿#region File Description
//-----------------------------------------------------------------------------
// CustomAvatarAnimationData.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
#if !WINDOWS_PHONE && !SILVERLIGHT
using Microsoft.Xna.Framework.GamerServices;
#endif
#endregion

namespace Brain
{
#if !WINDOWS_PHONE && !SILVERLIGHT && !MONOGAME
    /// <summary>
    /// The type contains the animation data for a single animation
    /// </summary>
    public class CustomAvatarAnimationData
    {
        /// <summary>
        /// The name of the animation clip
        /// </summary>
        [ContentSerializer]
        public string Name { get; private set; }

        /// <summary>
        /// The total length of the animation.
        /// </summary>
        [ContentSerializer]
        public TimeSpan Length { get; private set; }

        /// <summary>
        /// A combined list containing all the keyframes for all bones,
        /// sorted by time.
        /// </summary>
        [ContentSerializer]
        public List<AvatarKeyFrame> Keyframes { get; private set; }

        /// <summary>
        /// A combined list containing all the keyframes for expressions,
        /// sorted by time.
        /// </summary>
        [ContentSerializer]
        public List<AvatarExpressionKeyFrame> ExpressionKeyframes { get; private set; }

    #region Initialization

        /// <summary>
        /// Private constructor for use by the XNB deserializer.
        /// </summary>
        private CustomAvatarAnimationData() { }


        /// <summary>
        /// Constructs a new CustomAvatarAnimationData object.
        /// </summary>
        /// <param name="name">The name of the animation.</param>
        /// <param name="length">The length of the animation.</param>
        /// <param name="keyframes">The keyframes in the animation.</param>
        public CustomAvatarAnimationData(string name, TimeSpan length,
                                         List<AvatarKeyFrame> keyframes,
                                         List<AvatarExpressionKeyFrame> expressionKeyframes)
        {
            // safety-check the parameters
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (length <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("length",
                                          "The length of the animation cannot be zero.");
            }
            if ((keyframes == null) || (keyframes.Count <= 0))
            {
                throw new ArgumentNullException("keyframes");
            }

            // assign the parameters
            Name = name;
            Length = length;
            Keyframes = keyframes;
            ExpressionKeyframes = expressionKeyframes;
        }

        #endregion

        public static CustomAvatarAnimationData Read(ContentReader input, CustomAvatarAnimationData existingInstance)
        {
            CustomAvatarAnimationData data = new CustomAvatarAnimationData();

            data.Name = input.ReadString();

            data.Length = input.ReadObject<TimeSpan>();

            int framesCount = input.ReadInt32();
            data.Keyframes = new List<AvatarKeyFrame>(framesCount);
            for (int i = 0; i < framesCount; i++)
            {
                AvatarKeyFrame frame = new AvatarKeyFrame();
                frame.Bone = input.ReadInt32();
                frame.Time = input.ReadObject<TimeSpan>();
                frame.Transform = input.ReadMatrix();
                data.Keyframes.Add(frame);
            }

            bool hasExpression = input.ReadBoolean();
            if (hasExpression)
            {
                int expFramesCount = input.ReadInt32();
                data.ExpressionKeyframes = new List<AvatarExpressionKeyFrame>(expFramesCount);
                for (int i = 0; i < expFramesCount; i++)
                {
                    AvatarExpressionKeyFrame eframe = new AvatarExpressionKeyFrame();
                    eframe.Expression = input.ReadObject<AvatarExpression>();
                    eframe.Time = input.ReadObject<TimeSpan>();
                }
            }

            return data;
        }
    }
#endif
}
